﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DealKhuyenMaiV2.com.Models;
using PagedList;

namespace DealKhuyenMaiV2.com.Controllers
{
    [DealKhuyenMaiV2.com.Controllers.AccountController.AdminAuthorize]
    public class CommentManagerController : BaseController
    {

        //
        // GET: /CommentManager/

        public ViewResult Index()
        {

            var comments = DbConText.Comments.Include("Account").Include("Product");
            int pageSize = 10;
            ViewBag.Methol = "Index";
            ViewBag.Display = "Index";
            return View(comments.ToList().ToPagedList(1, pageSize));
        }
        [HttpPost]
        public PartialViewResult Index(int? page)
        {
            var comments = DbConText.Comments.Include("Account").Include("Product");
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            ViewBag.Methol = "Index";
            ViewBag.Display = "Index";
            return PartialView("_ListComment", comments.ToList().ToPagedList(pageNumber, pageSize));
        }
        [HttpPost]
        public PartialViewResult Search(int? page, string KeyWord, string Display)
        {
            var comments =
                DbConText.Comments.Include("Account").Include("Product").Where(
                          c => (c.Description.IndexOf(KeyWord) > -1
                          //|| KeyWord.IndexOf(c.Description) > -1
                          ));

            int pageSize = 10;
            int pageNumber = (page ?? 1);
            ViewBag.Methol = "Search";
            ViewBag.Display = Display;
            ViewBag.KeyWord = KeyWord;
            return PartialView("_ListComment", comments.ToList().ToPagedList(pageNumber, pageSize));
        }
     
        //
        // GET: /CommentManager/Create

        public ActionResult Create()
        {
            ViewBag.IDAccount = new SelectList(DbConText.Accounts, "IDAccount", "Email");
            ViewBag.IDProduct = new SelectList(DbConText.Products, "IDProduct", "NameProduct");
            return View();
        }

        //
        // POST: /CommentManager/Create

        [HttpPost]
        public ActionResult Create(Comment comment)
        {
            comment.DateAdder = DateTime.Now;
            if (ModelState.IsValid)
            {
                DbConText.Comments.AddObject(comment);
                DbConText.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.IDAccount = new SelectList(DbConText.Accounts, "IDAccount", "Email", comment.IDAccount);
            ViewBag.IDProduct = new SelectList(DbConText.Products, "IDProduct", "NameProduct", comment.IDProduct);
            return View(comment);
        }

        //
        // GET: /CommentManager/Edit/5

        public ActionResult Edit(int id)
        {
            Comment comment = DbConText.Comments.Single(c => c.IDComment == id);
           
            ViewBag.IDProduct = new SelectList(DbConText.Products, "IDProduct", "NameProduct", comment.IDProduct);
            return View(comment);
        }

        //
        // POST: /CommentManager/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Comment comment)
        {
            Comment _comment = DbConText.Comments.Single(c => c.IDComment == id);
            {
                _comment.Description = comment.Description;
            }
            if (ModelState.IsValid)
            {
                DbConText.SaveChanges();
                return RedirectToAction("Index");
            }
          
            ViewBag.IDProduct = new SelectList(DbConText.Products, "IDProduct", "NameProduct", comment.IDProduct);
            return View(comment);
        }

    
        [HttpPost, ActionName("Delete")]
        public PartialViewResult DeleteConfirmed(int? page, int id)
        {
            Comment comment = DbConText.Comments.Single(c => c.IDComment == id);
            DbConText.Comments.DeleteObject(comment);
            DbConText.SaveChanges();

            var comments = DbConText.Comments.Include("Account").Include("Product");
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            ViewBag.Methol = "Index";
            ViewBag.Display = "Index";
            return PartialView("_ListComment", comments.ToList().ToPagedList(pageNumber, pageSize));

        }
    }
}